/*************************************************************************
	> File Name: TcpServer.c
	> Author: 
	> Mail: 
	> Created Time: 2018年10月26日 星期五 15时48分42秒
 ************************************************************************/

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <pthread.h>
#include "../include/TCPserver.h"
//#include "sqlite.h"
#include "../include/gps.h"

int InitServer(void)
{
    int iServer = socket(AF_INET,SOCK_STREAM,0);
    if (ERROR == iServer)
    {
        return ERROR;
    }
    struct sockaddr_in stServer;
    memset(&stServer,0,sizeof(struct sockaddr_in));
    stServer.sin_family = AF_INET;
    stServer.sin_port = htons(8888);
    stServer.sin_addr.s_addr = inet_addr("192.168.1.196");
    int ret = bind(iServer,(struct sockaddr *)&stServer,sizeof(struct sockaddr));
    if (ERROR == ret)
    {
        return ERROR;
    }
    ret = listen(iServer,5);
    if (ERROR == ret)
    {
        return ERROR;
    }
    return iServer;
}

void AcceptClient(int serverSocket)
{
    if (serverSocket < 0)
    {
        return ;
    }
    struct sockaddr_in stClient;
    socklen_t clientSize = sizeof(struct sockaddr_in);
    char recMessage[BUF_SIZE] = {'\0'};
    char sendMessage[BUF_SIZE] = {'\0'};
    memset(&stClient,0,clientSize);
    int iClient = accept(serverSocket,(struct sockaddr *)&stClient,&clientSize);
    if (ERROR == iClient)
    {
        return ;
    }
    printf("accept success\r\n");
    while (1)
    {
        memset(recMessage,0,BUF_SIZE);
        memset(sendMessage,0,BUF_SIZE);
        int ret = recv(iClient,recMessage,BUF_SIZE,0);
        if (ret <= 0)
        {
            close(iClient);
            continue;
        }
        char getDB[BUF_SIZE] = "GETDB";
        char clrDB[BUF_SIZE] = "CLRDB";
        char regCD[BUF_SIZE] = "REGISTER";
        char getGPS[BUF_SIZE] = "GETGPS";
        if (!(strcmp(getDB,recMessage)))
        {
            printf("recv = %s\r\n",recMessage);
            select_table();
            long lSize = 0;
            char *readbuff = NULL;
            FILE *pFile = NULL;
            pFile = fopen("bus.txt","rb");
            if (NULL == pFile)
            {
                printf("file open error\r\n");
                close(iClient);
                return ;
            }
            fseek(pFile,0,SEEK_END);
            lSize = ftell(pFile);
            rewind(pFile);
            readbuff = (char *)malloc(sizeof(char) * lSize);
            memset(readbuff,0,(sizeof(char) * lSize));
            int result = fread(readbuff,1,lSize,pFile);
            if (lSize != result)
            {
                printf("reading error");
                free(readbuff);
                fclose(pFile);
                close(iClient);
                return ;
            }
            ret = send(iClient,readbuff,(sizeof(char) * lSize),0);
	    printf("send success! send = %d\r\n",ret);
            free(readbuff);
            fclose(pFile);
        }
        else if(!(strcmp(clrDB,recMessage)))
        {
            printf("recv = %s\r\n",recMessage);
            ret = clr_db();
            if (SUCCESS == ret)
            {
                printf("clear success\r\n");
            }
            else
            {
                printf("clear error\r\n");
            }
        }
        else if(!(strcmp("uxxx",recMessage)))
        {
            printf("recv = %s\r\n",recMessage);
            int x = get_ridership();
            char buf[BUF_SIZE] = {0};
            sprintf(buf,"当前客流量为：%d",x);
            printf("%s\r\n",buf);
            send(iClient,buf,sizeof(buf),0);
        }
        else if(!(strcmp(regCD,recMessage)))
        {
            printf("recv = %s\r\n",recMessage);
            char cardNum[BUF_SIZE] = {0};
            memset(cardNum,0,BUF_SIZE);
            int res = recv(iClient,cardNum,BUF_SIZE,0);
            if (res <= 0)
            {
                printf("recv cardnum error\r\n");
                close(iClient);
                continue;
            }
            int len = strlen(cardNum);
            char num = cardNum[len - 1];
            int type = num - '0';
            int i = 0;
            while (cardNum[i] != '#')
            {
                i++;
            }
            cardNum[i] = '\0';
            printf("cardnum = %s,type = %d\r\n",cardNum,type);
            ret = register_card(cardNum,type);
            if (SUCCESS == ret)
            {
                printf("register success\r\n");
            }
            else
            {
                printf("register error\r\n");
            }
        }
        else if(!(strcmp(getGPS,recMessage)))
        {
            printf("recv = %s\r\n",recMessage);
            char *gpsBuff = get_GPS();
            send(iClient,gpsBuff,BUF_SIZE,0);
            free(gpsBuff);
        }
        else
        {
            printf("recv = %s,recv error\r\n",recMessage);
            char error[BUF_SIZE] = "error";
            send(iClient,error,BUF_SIZE,0);
        }
    }
    close(iClient);
    close(serverSocket);
    return ;
}
